package com.gree.scmservice.controller;

import com.gree.common.annotation.Log;
import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.SysDictData;
import com.gree.common.entity.TablePageData;
import com.gree.common.enums.BusinessType;
import com.gree.common.utils.ExcelUtil;
import com.gree.common.utils.StringUtils;
import com.gree.scmservice.service.SysDictDataService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.nutz.lang.util.NutMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/dict/data")
@Api(tags = "系统信息:字典数据")
public class SysDictDataController {

    @Autowired
    private SysDictDataService sysDictDataService;

    @GetMapping("/list")
    @ApiOperation(value = "分页查询字典数据")
    @ApiImplicitParam(name = "dictData", value = "字典数据", dataType = "SysDictData")
    public ReturnVo list(SysDictData dictData, HttpServletRequest request)
    {
        TablePageData<SysDictData> sysDictDataTablePageData = sysDictDataService.selectDictDataList(dictData);
        return ReturnVo.success(sysDictDataTablePageData);

    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    @ApiOperation(value = "导出字典数据")
    @ApiImplicitParam(name = "logininfor", value = "用户登录日志", dataType = "SysLogininfor")
    public void export(SysDictData dictData, HttpServletRequest request, HttpServletResponse response)
    {
        TablePageData<SysDictData> sysDictDataTablePageData = sysDictDataService.selectDictDataList(dictData);
        List<SysDictData> list = sysDictDataTablePageData.getDataList();
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(list, "字典数据", response);
    }


    @GetMapping(value = "/{dictCode}")
    @ApiOperation(value = "获取字典数据")
    @ApiImplicitParam(name = "dictCode", value = "字典数据ID", dataType = "Long")
    public ReturnVo getInfo(@PathVariable Long dictCode)
    {
        return ReturnVo.success(sysDictDataService.selectDictDataById(dictCode));
    }


    /**
     * 新增字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation(value = "新增字典类型")
    @ApiImplicitParam(name = "dict", value = "字典数据", dataType = "SysDictData")
    public ReturnVo add(@Validated @RequestBody SysDictData dict)
    {
        int resultNum = sysDictDataService.insertDictData(dict);
        return ReturnVo.toAjax(resultNum);
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation(value = "修改保存字典类型")
    @ApiImplicitParam(name = "dict", value = "字典数据", dataType = "SysDictData")
    public ReturnVo edit(@Validated @RequestBody SysDictData dict)
    {
        int resultNum = sysDictDataService.updateDictData(dict);
        return ReturnVo.toAjax(resultNum);
    }

    /**
     * 删除字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{dictCodes}")
    @ApiOperation(value = "删除字典类型")
    @ApiImplicitParam(name = "dictCodes", value = "字典数据ID", dataType = "Long[]")
    public ReturnVo remove(@PathVariable Long[] dictCodes)
    {
        int resultNum = sysDictDataService.deleteDictDataByIds(dictCodes);
        return ReturnVo.toAjax(resultNum);
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    @ApiOperation(value = "根据字典类型查询字典数据信息")
    @ApiImplicitParam(name = "dictType", value = "数据字典类型", dataType = "String")
    public Object dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = sysDictDataService.selectDictDataByType(dictType);
        return ReturnVo.success(data);
    }

    /**
     * 清空缓存
     */
    @DeleteMapping("/clearCache")
    @ApiOperation(value = "清空缓存")
    public Object clearCache()
    {
        sysDictDataService.clearCache();
        return ReturnVo.success();
    }

}
